import { defineStore } from 'pinia';
import themeColor from '../theme/theme';
import { storage } from '../../libs/function/storage';
import type { tmVuetify, wxshareConfig, colorThemeType } from '../interface/interface';

const pdefault_cookies_color = storage.get('setTmVuetifyColor') || '';
const pdefault_cookies_black = storage.get('setTmVuetifyBlack');
const pdefault_cookies_local = storage.get('setTmVuetifyLocal') || 'zh-Hans';
const pdefault_cookies_colorArrayList = storage.get('colorArrayList');
const dark = typeof pdefault_cookies_black === 'boolean' ? pdefault_cookies_black : false;
let themeObj = new themeColor.themeColors();
if (pdefault_cookies_colorArrayList) {
    const result2 = pdefault_cookies_colorArrayList.filter((item: colorThemeType) =>
        themeObj.colors.every(subItem => subItem.name !== item.name),
    );
    themeObj = new themeColor.themeColors([...themeObj.colors, ...result2]);
}
const colorArray = themeObj.colors;
const os = uni.getSystemInfoSync()?.osName ?? '';
storage.set('colorArrayList', colorArray);
// 为 store state 声明类型
export interface State {
    tmVuetify: tmVuetify;
}

export const useTmpiniaStore = defineStore('tmpinia', {
    state: () => {
        return {
            tmStore: {
                color: pdefault_cookies_color,
                dark: dark,
                tmVueTifly_pages: '',
                tmVueTifly_pagesIndex: '',
                os: os,
                //这里是微信小程序和微信H5的配置资料。
                wxshareConfig_miniMp: {
                    title: '', // 分享标题
                    desc: '', // 描述
                    imageUrl: '', // 分享图片
                    path: '', // 分享路径
                    copyLink: '', // 复制链接
                    query: {}, // 分享参数
                },
                //当前存储存的主题对象。
                colorList: colorArray,
                //当前的语言
                local: pdefault_cookies_local,
            },
        };
    },
    actions: {
        setPageNow(url: string) {
            this.tmStore = {
                ...this.tmStore,
                tmVueTifly_pages: url,
            };
        },
        setPageNowIndex(index: string) {
            this.tmStore = {
                ...this.tmStore,
                tmVueTifly_pagesIndex: index,
            };
        },
        setTmVuetifyDark(dark: boolean) {
            dark = typeof dark !== 'boolean' ? false : dark;
            storage.set('setTmVuetifyBlack', dark);
            this.tmStore = {
                ...this.tmStore,
                dark: dark,
            };
        },
        setWxShare(cfg: wxshareConfig) {
            let pcf = cfg || {};
            if (typeof pcf !== 'object' || Array.isArray(cfg)) pcf = {};
            this.tmStore = {
                ...this.tmStore,
                wxshareConfig_miniMp: {
                    ...this.tmStore.wxshareConfig_miniMp,
                    ...pcf,
                },
            };
        },
        setTmVuetifyTheme(color: string) {
            let defaultColorName = color;
            if (!defaultColorName || defaultColorName == '' || themeColor.isCssColor(defaultColorName)) {
                defaultColorName = '';
            }
            storage.set('setTmVuetifyColor', defaultColorName);
            this.tmStore = { ...this.tmStore, color: defaultColorName };
        },
        //添加一个主题
        setTmVuetifyAddTheme(colorName: string, color: string, isSet = true) {
            this.tmStore = {
                ...this.tmStore,
                colorList: themeObj.add(colorName, color),
            };
            storage.set('colorArrayList', this.tmStore.colorList);
            if (isSet) {
                this.setTmVuetifyTheme(colorName);
            }
        },
        setTmLocal(language: string) {
            language = language || 'zh-Hans';
            storage.set('setTmVuetifyLocal', language);
            this.tmStore = {
                ...this.tmStore,
                local: language,
            };
        },
    },
});
